package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitqz.robot.api.business.entity.GoodsBuffersEntity;
import com.hitqz.robot.api.business.entity.GoodsGroupEntity;
import com.hitqz.robot.api.business.params.QueryGoodsGroupParams;
import com.hitqz.robot.api.business.vo.GoodsGroupVo;
import com.hitqz.robot.biz.service.GoodsBuffersService;
import com.hitqz.robot.biz.service.GoodsGroupService;
import com.hitqz.robot.common.core.util.R;
import com.hitqz.robot.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xupkun
 * @date 2025/1/2
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goodsGroup")
@Tag(name = "缓存架组管理")
@Slf4j
//@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsGroupController {

    private final GoodsGroupService goodsGroupService;

    private final GoodsBuffersService goodsBuffersService;

    @Operation(summary = "分页获取缓存架组")
    @GetMapping
    public R<Page<GoodsGroupVo>> page(@ParameterObject QueryGoodsGroupParams query) {
        Page<GoodsGroupEntity> page = new Page<>(query.getCurrent(),query.getSize());
        LambdaQueryWrapper<GoodsGroupEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.like(StrUtil.isNotEmpty(query.getName()), GoodsGroupEntity::getName, query.getName());
        wrapper.eq( query.getStatus() != null, GoodsGroupEntity::getName, query.getName());
        Page<GoodsGroupEntity> result = goodsGroupService.page(page, wrapper);
        Page<GoodsGroupVo> voPage = new Page<>(page.getCurrent(), page.getSize());
        List<GoodsGroupVo> robotVos = Lists.newArrayList();
        result.getRecords().forEach(t-> {
            GoodsGroupVo vo = new GoodsGroupVo();
            BeanUtil.copyProperties(t, vo, false);
            robotVos.add(vo);
        });
        voPage.setTotal(result.getTotal());
        voPage.setRecords(robotVos);
        return R.ok(voPage);
    }


    @Operation(summary = "根据id查询详情")
    @GetMapping("/{id}")
    public R<GoodsGroupVo> getById(@PathVariable Long id) {
        GoodsGroupEntity goodsGroupEntity = goodsGroupService.getById(id);
        GoodsGroupVo robotVos = new GoodsGroupVo();
        BeanUtil.copyProperties(goodsGroupEntity, robotVos, false);
        return R.ok(robotVos);
    }

    @Operation(summary = "获取缓存架数据")
    @GetMapping("list")
    public R<List<GoodsGroupVo>> list() {
        LambdaQueryWrapper<GoodsGroupEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(  GoodsGroupEntity::getStatus, 1);
        List<GoodsGroupEntity> result = goodsGroupService.list(wrapper);
        List<GoodsGroupVo> robotVos = Lists.newArrayList();
        result.forEach(t-> {
            GoodsGroupVo vo = new GoodsGroupVo();
            BeanUtil.copyProperties(t, vo, false);
            robotVos.add(vo);
        });
        return R.ok(robotVos);
    }

    @Operation(summary = "修改缓存架组")
    @PostMapping
    public R update(@RequestBody GoodsGroupEntity goodsGroupEntity) {
        goodsGroupService.updateById(goodsGroupEntity);
        return R.ok();
    }

    @Operation(summary = "添加缓存架组")
    @SysLog("增加缓存架组")
    @PutMapping
    public R add(@RequestBody GoodsGroupEntity goodsGroupEntity) {
        goodsGroupService.save(goodsGroupEntity);
        return R.ok();
    }


    @Operation(summary = "删除缓存架组")
    @SysLog("删除缓存架组")
    @DeleteMapping
    public R removeById(@RequestBody Long[] ids) {
        for(Long id : ids) {
            goodsBuffersService.remove(Wrappers.<GoodsBuffersEntity>lambdaQuery().eq(GoodsBuffersEntity::getGroupId, id));
            goodsGroupService.removeById(id);
        }
        return R.ok();
    }



}
